import Routes from '@/router/route'
import { RouteRecordRaw } from 'vue-router'

export default {
    namespaced : true,
    menus : [],
    
    actions : {
        buildMenu() {
            const menus:any = []
            function reduce(routes:RouteRecordRaw[], target:any[], prePath='') {
                routes.forEach(route => {
                //    if(rootState.login.auth) {
                //        if(route.name == 'login' || route.name == 'register') return
                //    }

                    if(route.meta && route.meta.hidden) return
                    let menu:any = {
                        name : route.meta?.title,
                    }

                    if(route.path == '') {
                        menu.path = prePath
                    }else {
                        if(prePath == '') {
                            menu.path = route.path
                        }else {
                            menu.path = prePath + '/' + route.path
                        }
                    }

                    target.push(menu)

                    if(route.children && route.children.length > 0) {
                        menu.children = []
                        reduce(route.children, menu.children, route.path)
                        if(menu.children.length == 0) {
                            delete menu.children
                        }else if(menu.children.length == 1) {
                            const child = menu.children[0]
                            menu.path = child.path
                            delete menu.children
                        }
                    }
                })
            }

            reduce(Routes, menus)
            return menus
        }
    },
}